package com.whd.demo.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whd.demo.common.Result;
import com.whd.demo.entity.WantRent;
import com.whd.demo.mapper.WantRentMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/wantRent")
public class WantRentController {

    @Resource
    WantRentMapper wantRentMapper;

    @PostMapping
    public Result<?> save(@RequestBody WantRent wantRent){
        wantRentMapper.insert(wantRent);
        return Result.success();
    }

    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search){
        LambdaQueryWrapper<WantRent> wrapper = Wrappers.<WantRent>lambdaQuery();
        Page<WantRent> wantRentPage = wantRentMapper.selectPage(new Page<>(pageNum,pageSize),wrapper);
        return Result.success(wantRentPage);
    }

    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id){
        wantRentMapper.deleteById(id);
        return Result.success();
    }

    @DeleteMapping("/cancel/{houseId}/{tenantId}")
    public Result<?> cancelWantRent(@PathVariable Integer houseId, @PathVariable Integer tenantId){
        wantRentMapper.cancelWantRent(houseId,tenantId);
        return Result.success();
    }

    @PutMapping
    public Result<?> update(@RequestBody WantRent wantRent){
        wantRentMapper.updateById(wantRent);
        return Result.success();
    }
}
